#include "CPole.h"
#include "CRelation.h"

namespace csmodel
{

    //---------------------------------

    CPole::CPole( CEntity * anOwner, int x, int y ) :
        inherited( anOwner, 0 ),
        directionType(""),
        signalType(""),
        pid( 0 )
    {
        if( anOwner )
            anOwner->Insert( this );
        Constcruct( x, y );
    }

    //---------------------------------

    void CPole::Draw( IDrawEngine & di ) const
    {
        QRect r( Bounds() );
        switch( dtype )
        {
            case IN:
                di.BoxIN( r );
                break;
            case OUT:
                di.BoxOUT( r );
                break;
            case DUAL:
                di.Box( r );
                break;
        }
    }

    //---------------------------------

    void CPole::Constcruct( int x, int y )
    {
        MoveTo( x, y );
        SetName( "Pole" );
        setDirection( DUAL );
        setSignalType( VARIANT );
        ResizeLocked( true );
    }

    //---------------------------------

    void CPole::GetProperties( CProperties * map )
    {
        inherited::GetProperties( map );
        map->insert( std::make_pair( "Direction", new CStrRefProperty< CPole >( this, &CPole::GetDirection,  &CPole::SetDirection, false ) ) );
        map->insert( std::make_pair( "SignalType", new CStrRefProperty< CPole >( this, &CPole::GetSignalType,  &CPole::SetSignalType, false ) ) );
    }

    //---------------------------------

    void CPole::SetDirection( const QString & dt )
    {
        DirectionType _dt;
        if( dt == "IN" )        _dt = IN;
        else if( dt == "OUT" )  _dt = OUT;
        else if( dt == "DUAL" ) _dt = DUAL;
        else
            throw std::logic_error( "CPole::SetDirection : NO SUCH DirectionType" );

        setDirection( _dt );
    }

    //---------------------------------

    void CPole::setDirection( DirectionType dt )
    {
        dtype = dt;
        switch( dtype )
        {
            case IN:
                directionType = "IN" ;
            break;

            case OUT:
                directionType = "OUT";
            break;

            case DUAL:
                directionType =  "DUAL";
            break;
        }
    }

    //---------------------------------

    void CPole::SetSignalType( const QString & pt )
    {
        PoolType _pt;
        if( pt == "BOOL" ) _pt = BOOL;
        else if( pt == "INT" )     _pt = INT;
        else if( pt == "UINT" )    _pt = UINT;
        else if( pt == "FLOAT" )   _pt = FLOAT;
        else if( pt == "LONG" )    _pt = LONG;
        else if( pt == "ULONG" )   _pt = ULONG;
        else if( pt == "STRING" )  _pt = STRING;
        else if( pt == "VARIANT" ) _pt = VARIANT;
        else
            throw std::logic_error( "CPole::SetSignalType : NO SUCH PoolType" );

        setSignalType( _pt );
    }

    //---------------------------------

    void CPole::setSignalType( PoolType pt )
    {
        ptype = pt;
        switch( ptype )
        {
            case BOOL:    signalType =  "BOOL"; break;
            case INT:     signalType =  "INT"; break;
            case UINT:    signalType =  "UINT"; break;
            case FLOAT:   signalType =  "FLOAT"; break;
            case LONG:    signalType =  "LONG"; break;
            case ULONG:   signalType =  "ULONG"; break;
            case STRING:  signalType =  "STRING"; break;
            case VARIANT: signalType =  "VARIANT"; break;
        }
    }

    //---------------------------------

    void CPole::addConnector( CRelation * c )
    {
        if( c )
        {
            RelIter it = relList.begin();
            while( it != relList.end() )
            {
                if( *it == c )
                    return;
                it++;
            }
            relList.push_back( c );
        }
    }

    //---------------------------------

    void CPole::rmConnector( CRelation * c )
    {
        if( c )
        {
            RelIter it = relList.begin();
            while( it != relList.end() )
            {
                if( *it == c )
                {
                    relList.erase( it );
                    return;
                }
                it++;
            }
        }
    }

    //---------------------------------

    ClassVisualTraits * CPole::visualTraits = 0;
}

